// pages/cut-in-side/cut-in-side.js
import WeCropper from '../../plug/we-cropper/we-cropper.js'
import { UploadImg } from '../../common/utils/index'
import { UPLOAD_IMAGE_URL, ERR_OK } from '../../common/config.js'
const { request, message, router, userInfo } = global;

const app = getApp()
const config = app.globalData.config

const device = wx.getSystemInfoSync()
const width = device.windowWidth
const height = device.windowHeight - 50
console.log(userInfo)
Page({
  data: {
    userInfo: userInfo,
    cropperOpt: {
      id: 'cropper',
      targetId: 'targetCropper',
      pixelRatio: device.pixelRatio,
      width,
      height,
      scale: 1,
      zoom: 8,
      cut: {
        x: (width - 280) / 2,
        y: (height - 280) / 2,
        width: 280,
        height: 280
      },
      boundStyle: {
        color: '#04b00f',
        mask: 'rgba(0,0,0,0.4)',
        lineWidth: 1
      }
    },
    id: ''
  },
  touchStart(e) {
    this.cropper.touchStart(e)
  },
  touchMove(e) {
    this.cropper.touchMove(e)
  },
  touchEnd(e) {
    this.cropper.touchEnd(e)
  },
  onGotUserInfo(e) {
    console.log(e.detail.userInfo)
    const { nickName, avatarUrl, gender } = e.detail.userInfo
    wx.login({
      success: res => {
        console.log('code is', res.code)
        request.post('/api/wx/xiaochengxu/login', {
          code: res.code,
          nickName,
          pic: avatarUrl,
          sex: gender
        }).then((res) => {
          const data = res.data
          if (data.code === ERR_OK) {
            global.userInfo = {
              nickName: data.rows[0].nickName,
              pic: data.rows[0].pic,
              sex: data.rows[0].sex
            };
            global.token = data.rows[0].token;
            global.openid = data.rows[0].openId;
            wx.setStorageSync('USER_INFO', global.userInfo);
            wx.setStorageSync('PET_TOKEN', global.token);
            wx.setStorageSync('PET_OPEN_ID', global.openid);
            this.getCropperImage()
          } else {
            message.tips('登录失败，请重试~');
          }
          console.log(res)
        }).catch((err) => {
          console.log(err)
        })
      }
    })
  },
  getCropperImage() {
    let { id } = this.data
    this.cropper.getCropperImage()
      .then((src) => {
        router.redirect({
          url: '/pages/iq-test/iq-test',
          data: {
            path: src,
            id
          }
        })
      })
      .catch((err) => {
        wx.showModal({
          title: '温馨提示',
          content: err.message
        })
      })
  },
  uploadTap() {
    const self = this

    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0]
        //  获取裁剪图片资源后，给data添加src属性及其值

        self.cropper.pushOrign(src)
      }
    })
  },
  onLoad(option) {
    let id = option.id
    this.setData({
      id
    })
    let src = option.path
    const { cropperOpt } = this.data
    cropperOpt.boundStyle.color = '#04b00f'
    this.setData({ cropperOpt })
    this.cropper = new WeCropper(cropperOpt)
      .on('ready', (ctx) => {
        console.log(`wecropper is ready for work!`)
      })
      .on('beforeImageLoad', (ctx) => {
        wx.showToast({
          title: '上传中',
          icon: 'loading',
          duration: 20000
        })
      })
      .on('imageLoad', (ctx) => {
        wx.hideToast()
      })
    this.cropper.pushOrign(src)
  }
})